package com.hhd.pdf.component.table.factory;

import com.hhd.pdf.CommonUtils;
import com.hhd.pdf.component.table.TableBorderHelper;
import com.hhd.pdf.component.table.TableVo;
import com.hhd.pdf.component.table.TdVo;
import com.hhd.pdf.component.table.TrVo;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TableHeaderFactory extends BaseTablePartFactory {
    @Override
    public void createTableElements(PdfPTable table,
                                    TableVo tableVo,
                                    GeneratorContext context,
                                    TableBorderHelper tableBorderHelper,
                                    Element element) {
        List<TrVo> trs = tableVo.getHeader();

        doCreateTableElements(table, trs, tableVo, context, tableBorderHelper);
    }

    private void doCreateTableElements(PdfPTable table,
                                       List<TrVo> trs,
                                       TableVo tableVo,
                                       GeneratorContext context,
                                       TableBorderHelper tableBorderHelper) {
        for (TrVo tr : trs) {
            doCreateTableElements(table, tr, tableVo, context, tableBorderHelper);
        }
    }

    private void doCreateTableElements(PdfPTable table,
                                       TrVo tr,
                                       TableVo tableVo,
                                       GeneratorContext context,
                                       TableBorderHelper tableBorderHelper) {
        List<TdVo> tds = tr.getTds();
        for (TdVo td : tds) {
            setDefaultAttribute(td.getTd(), "type", "header");
            PdfPCell cell = buildCell(td, tableBorderHelper, tableVo, context);
            GeneratorContext runtime = context.runtime(td.getTd());
            resolveDefaultStyle(td.getTd());

            List<com.itextpdf.text.Element> elements = genChildren(runtime, td.getTd());
            if (!CommonUtils.isEmpty(elements)) {
                elements.forEach(cell::addElement);
            }
            table.addCell(cell);
        }
        tableBorderHelper.nextRow();
    }

    private void resolveDefaultStyle(Element element) {
        setDefaultAttribute(element, "align", "center");
    }

    protected PdfPCell buildCell(TdVo td,
                                 TableBorderHelper tableBorderHelper,
                                 TableVo tableVo,
                                 GeneratorContext context) {
        PdfPCell cell = PdfUtils.createCell();
        cell.setPaddingLeft(5);
        cell.setPaddingRight(5);
        cell.setPaddingTop(0);
        cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
        cell.setColspan(td.getColSpan());

        cell.setHorizontalAlignment(td.getAlign());
        cell.setMinimumHeight(35);
        tableBorderHelper.setBorder(cell, tableVo, context);
        tableBorderHelper.nextCol();
        resolveTdStyle(td, cell);
        return cell;
    }

    private static void resolveTdStyle(TdVo td, PdfPCell cell) {
        try {
            Attribute type = td.getTd().attribute("type");
            if (type.getText().equals("header")) {
                cell.setBackgroundColor(PdfUtils.createColor(250, 250, 250));
            }
        } catch (Exception e) {
        }
    }

}
